package cn.coco.jf.fw.admin.sys.controller.user;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.jfinal.plugin.activerecord.Page;

import cn.coco.jf.fw.admin.config.consts.SysConsts;
import cn.coco.jf.fw.admin.controller.FwBaseController;
import cn.coco.jf.fw.admin.sys.dao.SysDao;
import cn.coco.jf.fw.admin.sys.dao.user.UserDao;
import cn.coco.jf.fw.admin.sys.model.user.UserModel;
import cn.coco.jf.fw.core.db.SysUserTbl;
import cn.exlu.coco.core.utils.CipherUtil;
import cn.exlu.coco.core.utils.StringUtil;

/**
 * 用户管理
 * 用户数据管理
 * 
 * @version 1.0.0
 * @author Mark
 * @see 参照者
 * @since 1.0.1.1
 * @date 2017年6月19日
 */
public class UserController extends FwBaseController {
    private static Logger _LOG = Logger.getLogger(UserController.class);
    
    /**
     * 进入页面
     */
    public void index(){
        this.setAttrJsonStr("roleDict", SysDao.queryNormalRoleDict());
        this.render("userIndex.html");

    }
    
    /**
     * 一览
     */
    public void doPage(){
        int pageNumber = this.getParaPageNumber();
        int pageSize = this.getParaPageSize();

        String userCode = this.getPara("userCode");
        String userName = this.getPara("userName");

        Page<UserModel> page = UserDao.page(pageNumber, pageSize, userCode, userName);
        
        pageJson(page);
    }
    
    public void select(){
        String userCode = this.getPara("userCode");
        // 更新的场合
        SysUserTbl entity = SysUserTbl.dao.findById(userCode);
        if(entity == null){
            _LOG.warn("记录不存在");
            this.errJson("记录不存在!");
            return;
        }
        
        entity.setUserPasswd(null);
        entity.setPasswdSalt(null);

        successJson(entity);
    }
    
    
    /**
     * 进入编辑页面
     */
    public void add(){
        this.setAttr("_oper", "add");
        this.render("userEdit.html");

    }
    
    public void doAdd(){

        SysUserTbl model = getBean(SysUserTbl.class, "");
        
        if(StringUtil.isNullOrEmpty(model.getUserCode())) {
        	this.errJson("请输入用户CODE");
            return;
        }
        
        if(model.getUserCode().contains("_")) {
        	this.errJson("用户CODE不允许输入含有_字符");
            return;
        }

        SysUserTbl entity = model.findById(model.getUserCode());
        if(entity != null){
            this.errJson("记录已存在");
            return;
        }

        if(StringUtil.isNullOrEmpty(model.getUserPasswd())){
            this.errJson("请输入密码");
            return;
        }

        String passwdSalt = StringUtil.createRandomSalt(SysConsts.PASSWD_SALT_LEN);
        model.setUserPasswd(CipherUtil.generatePassword(model.getUserCode() + model.getUserPasswd(), passwdSalt));
        model.setPasswdSalt(passwdSalt);
        model.setCreateUser(this.getLoginId());
        Date date = new Date();
        model.setCreateDateTime(date);
        model.setUpdateUser(this.getLoginId());
        model.setUpdateDateTime(date);
        model.setDelFlg(SysConsts.DEL_FLG_NORMAL);
        model.save();

        success(null);
        renderJson();
    }
    
    /**
     * 修改页面
     */
    public void upd(){
        this.setAttr("_oper", "upd");
        this.render("userEdit.html");
    }
    
    public void doUpd(){
        SysUserTbl model = getBean(SysUserTbl.class, "", true);
        
        SysUserTbl entity = model.findById(model.getUserCode());
        if(entity == null){
            this.errJson("记录不存在");
            return;
        }
        model.removeNullValueAttrs();
        if(!StringUtil.isNullOrBlank(model.getUserPasswd())){
//            model.setUserPasswd(entity.getUserPasswd());
            String passwdSalt = StringUtil.createRandomSalt(SysConsts.PASSWD_SALT_LEN);
            model.setUserPasswd(CipherUtil.generatePassword(model.getUserCode()+ model.getUserPasswd(), passwdSalt));
            model.setPasswdSalt(passwdSalt);
        }else{
            
        }

        model.update();

        success(null);
        renderJson();
    }
    
    public void doDel(){
        List<String> ids = this.getParaValuesStr("ids");

        if(ids.size() <=0){
            this.errJson("请选择一条记录!");
            return;
        }
        
        for(String id:ids){
            SysUserTbl model = SysUserTbl.newInstance();
            model.setUserCode(id);
            model.setDelFlg(SysConsts.DEL_FLG_DEL);
            model.update();
            
//            entity.delete();

        }


        success(null);
        renderJson();
    }
    
    
}
